package  katris.selenium.tests.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import java.util.List;

import katris.selenium.tests.model.Film;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class InternalPage extends AnyPage {

	public InternalPage(PageManager pages) {
		super(pages);
	}
	
  public InternalPage ensurePageLoaded() {
    super.ensurePageLoaded();
    wait.until(presenceOfElementLocated(By.cssSelector("nav")));
    return this;
  }
  
  @FindBy(css = "nav a[href $= '?go=profile']")
  private WebElement userProfileLink;

  @FindBy(css = "nav a[href $= '?go=users']")
  private WebElement userManagementLink;
  
  @FindBy(xpath = "//img[@title='Add movie']")
  private WebElement addMovieLink;

  @FindBy(css = "nav a[href $= '?logout']")
  private WebElement logoutLink;
  
  public UserProfilePage clickUserProfilePage() {
    userProfileLink.click();
    return pages.userProfilePage;
  }

  public UserManagementPage clickUserManagementLink() {
    userManagementLink.click();
    return pages.userManagementPage;
  }
  
  public LoginPage clickLogoutLink() {
    logoutLink.click();
    wait.until(alertIsPresent()).accept();
    return pages.loginPage;
  }
  
  public boolean descriptionIsPresent(Film film){
      List<WebElement> listOfFilms = wait.until(visibilityOfAllElementsLocatedBy(By.xpath("//div[@title='" + film.getTitle() + "']")));

      int numberOfFilms = listOfFilms.size();

      if (numberOfFilms!= 0) {

          if (numberOfFilms > 1) {
        	  System.out.println("The number of found Descriptions is " + numberOfFilms);
              return true;}
          else {System.out.println("Only one Description contains such name");
              return true;}
      }


      else {
    	  System.out.println("Description which contains the word " + film.getTitle() +" is not found");
          return false;}
  }
  
  public AddNewDescriptionPage clickAddMovieLink() {
	    addMovieLink.click();
	    return pages.addNewDescriptionPage;
	  }
  
  public ShortMovieInfoPage openMovieInfo(Film film) {
      WebElement selectFilm = wait.until(elementToBeClickable(By.xpath("//div[@title='" + film.getTitle() + "']")));
      selectFilm.click();
	    return pages.shortMovieInfoPage;
	  }


public boolean descriptionIsAbsent(Film film){
	
   // List<WebElement> listOfFilms = wait.until(presenceOfAllElementsLocatedBy(By.xpath("//div[@title='" + film.getTitle() + "']")));
	wait.until(invisibilityOfElementLocated(By.xpath("//div[@title='" + film.getTitle() + "']")));
      	  return true;
 
}

public boolean searchDescription(Film film) {


        WebElement search= new WebDriverWait(driver,20).until(elementToBeClickable(By.xpath("//input[@id='q']")));

        search.sendKeys(Keys.CONTROL + "a");
        search.sendKeys(Keys.DELETE);
        search.sendKeys(film.getTitle());

        search.sendKeys(Keys.RETURN);

        return true;
    }


public WebElement rememberItem(Film basicFilm){

        WebElement filmName = new WebDriverWait(driver,10).until(visibilityOfElementLocated(By.xpath("//div[@title='" + basicFilm.getTitle() + "']")));
      
        return filmName;
    }


public boolean waitUntilSearchFinish(WebElement filmData){

        wait.until(ExpectedConditions.stalenessOf(filmData));

        return true;
    }

public boolean cleanSearch() {


    WebElement search= new WebDriverWait(driver,20).until(elementToBeClickable(By.xpath("//input[@id='q']")));

    search.sendKeys(Keys.CONTROL + "a");
    search.sendKeys(Keys.DELETE);
    search.sendKeys(Keys.RETURN);

    return true;
}
}
